﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TweetMemeNET
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**********************************");
            Console.WriteLine("* TweetMeme.NET Test Application *");
            Console.WriteLine("**********************************\n");
            Console.WriteLine("Available options:");
            Console.WriteLine("1: URL info");
            Console.WriteLine("2: Tweets for URL");
            Console.WriteLine("3: Comments for URL");
            Console.WriteLine("4: Firehose, recent comments");
            Console.Write("\nEnter number 1-4: ");
            
            string choice = Console.ReadLine().Trim();

            Console.Clear();

            switch (choice)
            {
                case "1":
                    ExecuteUrlInfoProgram();
                    break;
                case "2":
                    ExecuteTweetsProgram();
                    break;
                case "3":
                    ExecuteCommentsProgram();
                    break;
                case "4":
                    ExecuteFirehoseProgram();
                    break;
                default:
                    Console.WriteLine("Invalid option.");
                    break;
            }

            Console.WriteLine("\nPress any key to exit the TweetMeme.NET console application...\n");
            Console.ReadKey(true);
        }

        static void ExecuteFirehoseProgram()
        {
            Console.Clear();

            Console.WriteLine("Retrieving firehose from TweetMeme...");

            List<Comment> comments = new TweetMemeCallFirehose().Result.Comments;

            Console.Clear();

            foreach (Comment c in comments)
            {
                Console.WriteLine(string.Format("User {0} commented '{1}' at {2}.\n", c.User.Username, c.Story.Title, c.Created));
            }
        }

        static void ExecuteCommentsProgram()
        {
            Console.Write("Enter site URL: ");

            // Get site URL from the user
            string siteUrl = Console.ReadLine();

            // Ensure a URL was specified
            if (string.IsNullOrEmpty(siteUrl))
            {
                Console.WriteLine("No URL specified. Program terminated.");
                return;
            }

            Console.Clear();

            Console.WriteLine("Retrieving comments from TweetMeme...");

            List<Comment> comments = TweetMeme.GetComments(siteUrl);

            Console.Clear();

            if (comments.Count>0)
            {
                foreach (Comment c in comments)
                {
                    Console.WriteLine(string.Format("User {0} commented '{1}' at {2}.\n", c.User.Username, c.Story.Title, c.Created));
                }
            }
            else
            {
                Console.WriteLine(string.Format("No comments found for '{0}'", siteUrl));
            }
        }

        static void ExecuteUrlInfoProgram()
        {
            Console.Write("Enter site URL: ");

            // Get site URL from the user
            string siteUrl = Console.ReadLine();

            // Ensure a URL was specified
            if (string.IsNullOrEmpty(siteUrl))
            {
                Console.WriteLine("No URL specified. Program terminated.");
                return;
            }

            Console.Clear();

            Console.WriteLine("Retrieving URL info from TweetMeme...");

            // Call the TweetMeme API
            TweetMemeResultUrlInfo result = TweetMeme.GetUrlInfo(siteUrl);

            Console.Clear();

            // Output results from the TweetMeme API
            if (result.Status==TweetMemeStatus.Success)
            {
                Console.WriteLine("URL info call successful!\n");

                // URL
                Console.WriteLine(string.Concat(("URL:").PadRight(20), result.Url));

                // Title
                Console.WriteLine(string.Concat(("Title:").PadRight(20), result.Title));

                // Created at
                Console.WriteLine(string.Concat(("Created:").PadRight(20), result.Created));

                // Media type
                Console.WriteLine(string.Concat(("Media type:").PadRight(20), result.MediaType.ToString()));

                // Linked on Twitter (retweets)
                Console.WriteLine(string.Concat(("Retweets:").PadRight(20), result.NumberOfRetweets, " times"));

                // Number of comments
                Console.WriteLine(string.Concat(("Comments:").PadRight(20), result.NumberOfComments, "\n"));
            }
            else // The API call failed
            {
                Console.WriteLine("TweetMeme API call failed.");
            }
        }

        static void ExecuteTweetsProgram()
        {
            Console.Write("Enter site URL: ");

            // Get site URL from the user
            string siteUrl = Console.ReadLine();

            // Ensure a URL was specified
            if (string.IsNullOrEmpty(siteUrl))
            {
                Console.WriteLine("No URL specified. Program terminated.");
                return;
            }

            Console.Clear();

            Console.WriteLine("Retrieving tweets from TweetMeme...");

            // Call the TweetMeme API
            List<Tweet> tweets = TweetMeme.GetTweets(siteUrl);

            Console.Clear();

            // Output tweets
            if (tweets.Count>0)
            {
                foreach (Tweet tweet in tweets)
                {
                    Console.WriteLine(string.Format("{0} ({1}) said: {2}\n", tweet.User.Username, tweet.User.Name, tweet.Text));
                }
            }
            else
            {
                Console.Write(string.Format("No tweets found for '{0}' in the last 7 days.\n", siteUrl));
            }
        }
    }
}
